package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.EventType;
import com.sl.au.entity.Hierarchy;
import com.sl.au.service.EventTypeService;
import com.sl.au.service.HierarchyService;

@RestController
@RequestMapping("/ui/hierarchyEventType")
public class HierarchyEventTypeController extends BaseController {

	@Autowired
	private HierarchyService hierarchyService;
	@Autowired
	private EventTypeService eventTypeService;

	@RequestMapping(value = "/initInnerEventType", method = RequestMethod.POST)
	public Map<String, Object> initInnerEventType(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("hierarchyId");
		String con = request.getParameter("con");
		List<EventType> infos = null;
		int count = 0;
		if ("0".equals(key)) {// 查询所有
			if (StringUtils.hasText(con)) {
				infos = eventTypeService.findInnerTypeOnlyCon(start, length, con);
				count = eventTypeService.findInnerTypeCountOnlyCon(con);
			} else {
				infos = eventTypeService.findInnerTypeNone(start, length);
				count = eventTypeService.findInnerTypeCountNone();
			}
		} else {// 查询指定树节点
			if (StringUtils.hasText(con)) {
				infos = eventTypeService.findInnerTypeByKeyAndCon(start, length, key, con);
				count = eventTypeService.findInnerTypeCountByKeyAndCon(key, con);
			} else {
				infos = eventTypeService.findInnerTypeOnlyKey(start, length, key);
				count = eventTypeService.findInnerTypeCountOnlyKey(key);
			}
		}
		map.put("data", infos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	@RequestMapping(value = "/initOuterEventType", method = RequestMethod.POST)
	public Map<String, Object> initOuterEventType(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("hierarchyId");
		String con = request.getParameter("con");
		List<EventType> infos = null;
		int count = 0;
		if ("0".equals(key)) {// 查询所有
			if (StringUtils.hasText(con)) {
				infos = eventTypeService.findOuterTypeOnlyCon(start, length, con);
				count = eventTypeService.findOuterTypeCountOnlyCon(con);
			} else {
				infos = eventTypeService.findOuterTypeNone(start, length);
				count = eventTypeService.findOuterTypeCountNone();
			}
		} else {// 查询指定树节点
			if (StringUtils.hasText(con)) {
				infos = eventTypeService.findOuterTypeByKeyAndCon(start, length, key, con);
				count = eventTypeService.findOuterTypeCountByKeyAndCon(key, con);
			} else {
				infos = eventTypeService.findOuterTypeOnlyKey(start, length, key);
				count = eventTypeService.findOuterTypeCountOnlyKey(key);
			}
		}
		map.put("data", infos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	/**
	 * 显示组织结构
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/initHierarchyTree", method = RequestMethod.GET)
	public Map<String, Object> initHierarchyTree(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Hierarchy> node = hierarchyService.getAllNodes();
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();

		nodeMap = hierarchyService.getNodeMap(node);

		map.put("code", 1);
		map.put("nodes", nodeMap);

		return map;
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView) {
		modelAndView = createLayoutView("hierarchyEventType/index");
		return modelAndView;
	}
}
